package com.sky.controller.admin;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShopCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user/shoppingCart")
public class ShopCartController {

    @Autowired
    private ShopCartService shopCartService;

    /**
     * 查看购物车
     *
     * @return
     */
    @GetMapping("list")
    public Result<List<ShoppingCart>> findShopCart() {
        log.info("查看购物车");
        List<ShoppingCart> shoppingCarts = shopCartService.findShopCart();
        return Result.success(shoppingCarts);
    }


    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     * @return
     */
    @PostMapping("add")
    public Result addShopCart(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        log.info("添加购物车:{},{},{}", shoppingCartDTO.getDishFlavor(), shoppingCartDTO.getDishId(), shoppingCartDTO.getSetmealId());
        shopCartService.addShopCart(shoppingCartDTO);
        return Result.success();
    }


    /**
     * 删除购物车中一个商品
     *
     * @param shoppingCartDTO
     * @return
     */
    @PostMapping("sub")
    public Result deleteById(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        log.info("删除购物车中一个商品:{}", shoppingCartDTO);
        shopCartService.deleteById(shoppingCartDTO);
        return Result.success();
    }

    /**
     * 清空购物车
     *
     * @return
     */
    @DeleteMapping("clean")
    public Result deleteAll() {
        log.info("清空购物车");
        shopCartService.deleteAll();
        return Result.success();
    }
}
